<?php
use Illuminate\Database\Capsule\Manager as DB;

#[店铺轮播图管理]
class BannerController extends McommonController{

    public function init()
    {
        parent::init();
        $this->table = 'dianpu_banner';
        $this->primaryKey = 'id';
        $this->model = (new \app\modules\Merchant\models\bannerModel())->where('dianpu_id', $this->auth->dianpu_id);
    }

    #[轮播图列表]
    public function indexAction($returnData = FALSE)
    {
        if($this->isPost()){
            $params = $this->postData;
            $query = $this->model;
            if(isset($params['mode']) && !empty($params['mode']) && $params['mode'] !== 'all'){
                switch ($params['mode']){
                    case 'nouse':
                        $query = $query->where('status', 0);
                        break;
                    case 'inuse':
                        $query = $query->where('status', 1);
                        break;
                }
            }
            if (isset($params['apply_start']) && !empty($params['apply_start'])) {
                $query = $query->whereDate('created_at', '>=', $params['apply_start']);
            }
            if (isset($params['apply_end']) && !empty($params['apply_end'])) {
                $query = $query->whereDate('created_at', '<=', $params['apply_end']);
            }
            $total = $query->count('id');
            $query = $query->orderBy('sort', 'DESC')->orderBy('id', 'DESC');
            $page = $this->get('page', 1);
            $limit = $this->get('rows', 10);
            $offset = ($page - 1) * $limit;
            $rows = $query->offset($offset)->limit($limit)->get();
            json(['total' => $total, 'rows' => $rows]);
        }else{
            return parent::indexAction($returnData); // TODO: Change the autogenerated stub
        }
    }

    #[编辑轮播图]
    public function editAction()
    {
        if($this->isPost()){
            do {
                $rows = $this->postData;
                if (!isset($rows[$this->primaryKey]) && isset($rows['dataset']) && is_array($rows['dataset'])) {
                    $rows = $rows['dataset'];
                }
                $roles = [
                    $this->primaryKey => ['role' => "required|exists:{$this->table}.{$this->primaryKey}", 'msg' => '主键'],
                ];
                Validate::check($rows, $roles);
                if(empty($rows['image'])){
                    throw new Exception('没有上传图片');
                }
                if(empty($rows['title'])){
                    throw new Exception('没有轮播图标题');
                }
                $show_num = $this->model->where('status', 1)->count('id');
                if($show_num > 4){
                    $rows['status'] = 0;
                }
                if($rows['dianpu_id'] != $this->auth->dianpu_id){
                    throw new Exception('不能修改其他人的内容');
                }
                $rows['sort'] = $rows['sort']?:500;
                $rows['updated_at'] = timestamp();
                unset($rows['created_at']);
                $this->cleanColumn($rows);
                if (\app\modules\Merchant\models\bannerModel::query()->where($this->primaryKey, $rows[$this->primaryKey])->update($rows) !== FALSE) {
                    $result = [
                        'ret' => 0,
                        'msg' => '操作成功',
                    ];
                } else {
                    $result = [
                        'ret' => 3,
                        'msg' => '更新失败',
                    ];
                }
            } while (FALSE);
            json($result);
        }else{
            parent::editAction(); // TODO: Change the autogenerated stub
        }
    }

    #[新增轮播图]
    public function addAction()
    {
        if ($this->isPost()){
            do {
                $rows = $this->postData;
                $rows['created_at'] = date('Y-m-d H:i:s');
                unset($rows[$this->primaryKey], $rows['updated_at'], $rows['deleted_at']);
                $this->cleanColumn($rows);
                if(empty($rows['image'])){
                    throw new Exception('没有上传图片');
                }
                if(empty($rows['title'])){
                    throw new Exception('没有轮播图标题');
                }
                $show_num = $this->model->where('status', 1)->count('id');
                if($show_num > 4){
                    $rows['status'] = 0;
                }
                $rows['sort'] = $rows['sort']?:500;
                $rows['dianpu_id'] = $this->auth->dianpu_id;
                if ($insert_id = \app\modules\Merchant\models\bannerModel::query()->insertGetId($rows)) {
                    $result = [
                        'ret' => 0,
                        'msg' => '操作成功',
                        'data' => $insert_id,
                    ];
                } else {
                    $result = [
                        'ret' => 3,
                        'msg' => '数据插入失败',
                    ];
                }
            } while (FALSE);
            json($result);
        }else{
            parent::addAction(); // TODO: Change the autogenerated stub
        }
    }

    #[删除轮播图]
    public function deleteAction()
    {
        $banner = \app\modules\Merchant\models\bannerModel::query()->find($this->getPost('id'));
        if(!$banner){
            throw new Exception('轮播图不存在');
        }
        if($banner->dianpu_id != $this->auth->dianpu_id){
            throw new Exception('不能删除其他人的内容');
        }
        parent::deleteAction(); // TODO: Change the autogenerated stub
    }
}
